﻿using HCMS.HttpApi.Site.Filter;
using HCMS.Utils;
using Microsoft.Extensions.WebEncoders;
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace HCMS.HttpApi.Site.ServicesExtensions
{
    /// <summary>
    /// 服务 核心扩展
    /// </summary>
    public static class CoreExtension
    {
        /// <summary>
        /// MVC 扩展
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddCustomMVC(this IServiceCollection services)
        {
            var mvcBuilder = services.AddControllers(options =>
                            {
                                // 添加 全局过滤器
                                options.Filters.Add<GlobalExceptionFilter>();
                            }).AddJsonOptions(options =>
                                // Json 序列化 增加 中文处理，设置后，输出到页面的中文内容才不会被编码。
                                // 例如：原来显示【\u5FAE\u4FE1\u5C0F\u7A0B\u5E8F】，设置后可以正常显示【微信小程序】
                                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                            );

            return services;
        }

        /// <summary>
        /// Redis 配置
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddCustomRedisContext(this IServiceCollection services)
        {
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = ConfigUtil.GetValue("ConnectionStrings:Redis");
                options.InstanceName = "HcmsApi";
            });

            return services;
        }
    }
}
